/*
 * @Author: ItKui<13452903663@163.com>
 * @Date: 2020-10-16 08:58:27
 * @LastEditTime: 2020-10-17 10:05:12
 * @FilePath: \wx-debuger\app.js
 * @FileName:
 */
var app = require('koa')(),
  logger = require('koa-logger'),
  json = require('koa-json'),
  views = require('koa-views'),
  onerror = require('koa-onerror');

var index = require('./routes/index');
var api = require('./routes/api');

// cache
// const session = require('koa-generic-session');
// const Redis = require('koa-redis');
// app.keys = ['keys', 'keyskeys'];
// app.use(function* (next) {
//   session({
//     store: new Redis(),
//   })(this, next);
//   yield next;
// });

// error handler
onerror(app);

// global middlewares
app.use(
  views('views', {
    root: __dirname + '/views',
    default: 'jade',
  }),
);
app.use(require('koa-bodyparser')());
app.use(json());
app.use(logger());

app.use(function* (next) {
  var start = new Date();
  yield next;
  var ms = new Date() - start;
  console.log('%s %s - %s', this.method, this.url, ms);
});

app.use(require('koa-static')(__dirname + '/public'));

// routes definition
app.use(index.routes(), index.allowedMethods());
app.use(api.routes(), api.allowedMethods());

// error-handling
app.on('error', (err, ctx) => {
  console.error('server error', err, ctx);
});

module.exports = app;
